package leetCode;

import org.junit.Test;

/**
 * TODO
 *
 * @author 司松坡
 * @createTime 2023年03月20日
 */
public class LeetCode38 {
    //不打表
    public  String countAndSay(int n) {
        String res = String.valueOf(1);
        if (n == 1) {
            return res;
        }
        for (int i = 1; i < n; i++) {
            res = say(res);
        }
        return res;
    }

    public   String say(String res) {
        int length = res.length();
        char c = res.charAt(0);
        int count = 1;
        StringBuilder stringBuffer = new StringBuilder();
        for (int i = 1; i < length; i++) {
            if (c == res.charAt(i)){
                count++;
            } else {
                stringBuffer.append(count);
                stringBuffer.append(c);
                c = res.charAt(i);
                count = 1;
            }
        }
        stringBuffer.append(count);
        stringBuffer.append(c);
        return String.valueOf(stringBuffer);
    }

    @Test
    public void test(){
        System.out.println(countAndSay(6));

    }
}
